const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const models = require('../db/models')

app.use(express.json())
app.use(express.urlencoded())
app.use(bodyParser.urlencoded({ extended: true }))

// 查询任务列表
app.post('/list', async (req, res, next) => {
    try {
        console.log('req', req)
        // let { status, page } = req.body.data
        let { status } = req.body
        console.log('status', status)
        // let limit = 10
        // let offset = (page - 1) * 10
        // 状态 0查看全部 1待办 2正在进行 3完成
        // 分页处理
        let where = {}
        if (status !== '0') {
            where = {
                status
            }
        }
        let list = await models.Todo.findAndCountAll({
            where,
            // offset,
            // limit
        })
        res.json({
            list,
            message: '列表查询完成'
        })
    } catch (error) {
        
    }
})
// 新增任务
app.post('/create', async (req, res, next) => {
    try {
        // console.log('req', req)
        let { name, status, deadline, content } = req.body.data
        let dbName = await models.Todo.findAndCountAll({
            where: {
                name
            }
        })
        // let dbName = ''
        // console.log(dbName)
        if (dbName.count) {
            res.json({
                type: 1,
                message: '任务已存在'
            })
        } else {
            let todo = await models.Todo.create({
                name,
                status,
                deadline,
                content
            })
            res.json({
                todo,
                message: '任务创建成功'
            })
        }
        
    } catch (error) {
        next(error)
    }
})
// 编辑任务和修改任务状态
app.post('/update', async (req, res, next) => {
    try {
        let { id, name, deadline, content, status } = req.body.data
        let todo = await models.Todo.findOne({
            where:{
                id
            }
        })
        if (todo) {
            todo = todo.update({
                name,
                deadline,
                content,
                status
            })
        }
        res.json({
            todo,
            message: '修改成功'
        })
    } catch (error) {
        next(error)
    }
})
// 删除任务
app.get('/delete/:id', async (req, res, next) => {
    try {
        let { id } = req.params
        let todo = await models.Todo.findOne({
            where:{
                id
            }
        })
        if (todo) {
            todo.destroy()
            res.json({
                id,
                message: '删除成功'
            })
        } else {
            res.json({
                id,
                message: '没有该条数据'
            })
        }
    } catch (error) {
        next(error)
    }
})


// 全局异常处理
app.use((err, req, res) => {
    if (err) {
        res.status(500).json({
            message: err.message
        })
    }
})

app.listen(3001, () => {
    console.log('服务启动成功')
})